home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2008 February / PC_Format_022008.iso / Internet / Mozilla Thunderbird wtyczki / lightning-0.7-tb-win.xpi / chrome / lightning.jar / content / lightning / messenger-overlay-toolbar.js < prev    next >
Encoding:
JavaScript  |  2007-09-22  |  12.6 KB  |  365 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Sun Microsystems code.
  15.  *
  16.  * The Initial Developer of the Original Code is Sun Microsystems.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Michael Buettner <michael.buettner@sun.com>
  22.  *   Philipp Kewisch <mozilla@kewis.ch>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. /**
  39.  * Global variables
  40.  */
  41. var gCustomizeId;
  42.  
  43. /**
  44.  * the current mode is set to a string defining the current
  45.  * mode we're in. allowed values are:
  46.  *  - 'mode'
  47.  *  - 'mail'
  48.  *  - 'calendar'
  49.  *  - 'task'
  50.  */
  51. var gCurrentMode = 'mail';
  52.  
  53. /**
  54.  * Helper function to get the view deck in a neutral way, regardless of whether
  55.  * we're in Thunderbird 1.x or 2.x
  56.  */
  57. function getMailBar() {
  58.   return document.getElementById("mail-bar2") ||
  59.          document.getElementById("mail-bar");
  60. }
  61.  
  62. /**
  63.  * Ensure that switching to the messenger window also switches to mail mode.
  64.  * We probably should also catch this from other windows (compose, addressbook),
  65.  * but for now we'll keep it here. This function overrides the toMessengerWindow
  66.  * function in /mail/base/content/mailCore.js.
  67.  */
  68. var toMessengerWindow = function ltnToMessengerWindow() {
  69.     var wm = Components.classes['@mozilla.org/appshell/window-mediator;1']
  70.              .getService(Components.interfaces.nsIWindowMediator);
  71.  
  72.     var topWindow = wm.getMostRecentWindow("mail:3pane");
  73.  
  74.     if (topWindow) {
  75.         var tomail = topWindow.document.getElementById("switch2mail");
  76.         tomail.doCommand();
  77.         topWindow.focus();
  78.     } else {
  79.         window.open("chrome://messenger/content/messenger.xul",
  80.                     "_blank",
  81.                     "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar");
  82.     }
  83. };
  84.  
  85. /**
  86.  * ltnSwitch2Mail() switches to the mail mode
  87.  */
  88.  
  89. function ltnSwitch2Mail() {
  90.   if (gCurrentMode != 'mail') {
  91.  
  92.     var switch2mail = document.getElementById("switch2mail");
  93.     var switch2calendar = document.getElementById("switch2calendar");
  94.     switch2mail.setAttribute("checked", "true");
  95.     switch2calendar.removeAttribute("checked");
  96.  
  97.     gCurrentMode = 'mail';
  98.  
  99.     var mailToolbar = getMailBar();
  100.     var calendarToolbar = document.getElementById("calendar-toolbar");
  101.     mailToolbar.removeAttribute("collapsed");
  102.     calendarToolbar.setAttribute("collapsed", "true");
  103.  
  104.     // the content panel should display the folder tree
  105.     var contentDeck = document.getElementById("contentPanel");
  106.     contentDeck.selectedPanel = document.getElementById("folderPaneBox");
  107.     
  108.     // tell thunderbird that it needs to refresh the mail list.
  109.     // basically, we fake a selection change by directly calling
  110.     // the appropriate handler while clearing out some internal
  111.     // variables in order to force a refresh of the mail views.
  112.     gMsgFolderSelected = null;
  113.     msgWindow.openFolder = null;
  114.     ShowThreadPane();
  115.     FolderPaneSelectionChange();
  116.     
  117.     document.commandDispatcher.updateCommands('mail-toolbar');
  118.     document.commandDispatcher.updateCommands('calendar_commands');
  119.   }
  120. }
  121.  
  122. /**
  123.  * ltnSwitch2Calendar() switches to the calendar mode
  124.  */
  125.  
  126. function ltnSwitch2Calendar() {
  127.   if (gCurrentMode != 'calendar') {
  128.  
  129.     var switch2mail = document.getElementById("switch2mail");
  130.     var switch2calendar = document.getElementById("switch2calendar");
  131.     switch2calendar.setAttribute("checked", "true");
  132.     switch2mail.removeAttribute("checked");
  133.  
  134.     gCurrentMode = 'calendar';
  135.  
  136.     var mailToolbar = getMailBar();
  137.     var calendarToolbar = document.getElementById("calendar-toolbar");
  138.     mailToolbar.setAttribute("collapsed", "true");
  139.     calendarToolbar.removeAttribute("collapsed");
  140.  
  141.     // the content deck should display the calendar panel
  142.     var contentDeck = document.getElementById("contentPanel");
  143.     contentDeck.selectedPanel = document.getElementById("ltnSidebar");
  144.  
  145.     // display the calendar panel on the display deck
  146.     var deck = document.getElementById("displayDeck");
  147.     deck.selectedPanel = document.getElementById("calendar-view-box");
  148.  
  149.     // show the last displayed type of calendar view
  150.     showCalendarView(gLastShownCalendarView);
  151.  
  152.     document.commandDispatcher.updateCommands('mail-toolbar');
  153.     document.commandDispatcher.updateCommands('calendar_commands');
  154.   }
  155. }
  156.  
  157. /**
  158.  * ltnSwitch2Task() switches to the task mode
  159.  */
  160.  
  161. function ltnSwitch2Task() {
  162.   if (gCurrentMode != 'task') {
  163.  
  164.     document.commandDispatcher.updateCommands('mail-toolbar');
  165.     document.commandDispatcher.updateCommands('calendar_commands');
  166.   
  167.     gCurrentMode = 'task';
  168.   }
  169. }
  170.  
  171. /**
  172.  * CustomizeApplicationToolbar() is called to customize one of the toolbars.
  173.  * the appropriate identifier is passed as argument to the function.
  174.  */
  175.  
  176. // this shadows CustomizeMailToolbar from mail/base/content/mailCore.js
  177. // but adds the specific bits and pieces for lightning.
  178. function CustomizeApplicationToolbar(id) {
  179.   // the following code operates different whether
  180.   // or not we're actually customizing the mode toolbar or
  181.   // any other toolbar.
  182.   gCustomizeId = id;
  183.   var isModeToolbox = (id == 'mode-toolbox');
  184.   var modeName = isModeToolbox ? 'mode' : gCurrentMode;
  185.  
  186.   // retrieve the toolbars from the tree
  187.   var mailbar = getMailBar();
  188.   var menubar = document.getElementById('mail-menubar');
  189.   var calendarbar = document.getElementById('calendar-toolbar');
  190.   var mailbox = document.getElementById("mail-toolbox");
  191.   var modebar = document.getElementById('mode-toolbar');
  192.   var modebox = document.getElementById('mode-toolbox');
  193.  
  194.   // install the callback that handles what needs to be
  195.   // done after a toolbar has been customized.
  196.   if (modebox) {
  197.     mailbox.customizeDone = ModeToolboxCustomizeDone;
  198.     modebox.customizeDone = ModeToolboxCustomizeDone;
  199.  
  200.     // disable elements on the toolbars
  201.     if (isModeToolbox) {
  202.       EnableDisableHierarchy(menubar, true);
  203.       EnableDisableHierarchy(mailbar, true);
  204.       EnableDisableHierarchy(calendarbar, true);
  205.     } else {
  206.       EnableDisableHierarchy(modebar, true);
  207.     }
  208.   } else {
  209.     modeName = null;
  210.   }
  211.  
  212.   var customizePopup = document.getElementById("CustomizeMailToolbar"); 
  213.   customizePopup.setAttribute("disabled", "true");
  214.  
  215.   var wintype = document.documentElement.getAttribute("windowtype");
  216.   wintype = wintype.replace(/:/g, "");
  217.  
  218.   // lightning install a new dropdown list in the customize dialog
  219.   // which allows to switch to all available toolbars while still
  220.   // being in the dialog. in case a new entry has been selected this
  221.   // function will be called. the argument will "mode", "mail",
  222.   // "calendar" or "task".
  223.   var onModeSwitch = function switchHandler(aMode) {
  224.     
  225.     // assume that we're switching to the mode toolbar
  226.     var toolbox = 'mode-toolbox';
  227.  
  228.     // check which toolbar is to be customized next
  229.     // and possibly switch the the appropriate mode.    
  230.     if(aMode == 'mail') {
  231.       ltnSwitch2Mail();
  232.       toolbox = 'mail-toolbox';
  233.     } else if(aMode == 'calendar') {
  234.       ltnSwitch2Calendar();
  235.       toolbox = 'mail-toolbox';
  236.     }
  237.  
  238.     // enable/disable all toolbar to reflect the new state
  239.     var isMode = (aMode == 'mode');
  240.     EnableDisableHierarchy(modebar, !isMode);
  241.     EnableDisableHierarchy(menubar, isMode);
  242.     EnableDisableHierarchy(mailbar, isMode);
  243.     EnableDisableHierarchy(calendarbar, isMode);
  244.     
  245.     // remember the current toolbox
  246.     gCustomizeId = toolbox;
  247.     
  248.     // return this appropriate toolbox element
  249.     return document.getElementById(toolbox);
  250.   };
  251.  
  252.   // open the customize toolbar dialog now...
  253.   window.openDialog("chrome://global/content/customizeToolbar.xul",
  254.                     "CustomizeToolbar"+wintype,
  255.                     "chrome,all,dependent",
  256.                       document.getElementById(id),  // toolbar dom node
  257.                       isModeToolbox,                // is mode toolbar yes/no?
  258.                       onModeSwitch,                 // callback function
  259.                       modeName);                    // name of this mode
  260. }
  261.  
  262. /**
  263.  * ModeToolboxCustomizeDone() is called after the customize toolbar dialog
  264.  * has been closed by the user. We need to restore the state of all buttons
  265.  * and commands of all customizable toolbars.
  266.  */
  267.  
  268. function ModeToolboxCustomizeDone(aToolboxChanged) {
  269.   // the following code operates different whether
  270.   // or not we're actually customizing the mode toolbar or
  271.   // any other toolbar.
  272.   var isModeToolbox = (gCustomizeId == 'mode-toolbox');
  273.   
  274.   // enable elements on the toolbars
  275.   if (isModeToolbox) {
  276.     EnableDisableHierarchy(document.getElementById('mail-menubar'), false);
  277.     EnableDisableHierarchy(getMailBar(), false);
  278.     EnableDisableHierarchy(document.getElementById('calendar-toolbar'), false);
  279.   }
  280.  
  281.   // Unconditionally enable the mode toolbar
  282.   EnableHierarchy(document.getElementById('mode-toolbar'));
  283.   
  284.   // Update global UI elements that may have been added or removed
  285.   MailToolboxCustomizeDone(aToolboxChanged);
  286.   
  287.   // make sure our toolbar buttons have the correct enabled state restored to them...
  288.   document.commandDispatcher.updateCommands('calendar_commands');
  289. }
  290.  
  291. /**
  292.  * EnableDisableHierarchy() recursively walks the dom tree and enables or disables
  293.  * all elements it encounters. this function is used by ModeToolboxCustomizeDone()
  294.  * and CustomizeApplicationToolbar().
  295.  */
  296.  
  297. // step along the hierarchy where the top-node is to be passed
  298. // as argument and enable/disable all nodes depending on the given flag.
  299. function EnableDisableHierarchy(item, disable) {
  300.   // iterate all nodes on this particular level
  301.   for (var i = 0; i < item.childNodes.length; ++i) {
  302.   
  303.     // retrieve the next node that needs to be processed
  304.     var child = item.childNodes[i];
  305.     
  306.     // disable this node if flag indicates this case, enable otherwise
  307.     if (disable) {
  308.  
  309.       // in case this node has already been disabled, we remember
  310.       // this fact in the 'itemdisabled' attribute in order
  311.       // to restore the original state at the end of the operation.
  312.       if (child.getAttribute("disabled") == "true") {
  313.         child.setAttribute("itemdisabled", "true");
  314.       }
  315.       child.setAttribute("disabled","true");
  316.       
  317.     } else {
  318.     
  319.       // restore the previous state, which means either enable
  320.       // the node or keep it disabled but remove the
  321.       // 'itemdisabled' attribute.
  322.       if(child.hasAttribute("itemdisabled")) {
  323.         child.removeAttribute("itemdisabled");
  324.       } else {
  325.         child.removeAttribute("disabled");
  326.       }
  327.     }
  328.  
  329.     // recursively step down the hierarchy if this node
  330.     // exposes any further child nodes.
  331.     if (child.childNodes.length > 0) {
  332.       EnableDisableHierarchy(child, disable);
  333.     }
  334.   }
  335. }
  336.  
  337. /**
  338.  * EnableHierarchy() recursively walks the dom tree and enables
  339.  * all elements it encounters. this function is used by ModeToolboxCustomizeDone().
  340.  */
  341.  
  342. // step along the hierarchy where the top-node is to be passed
  343. // as argument and enable all nodes unconditionally.
  344. function EnableHierarchy(item) {
  345.   // iterate all nodes on this particular level
  346.   for (var i = 0; i < item.childNodes.length; ++i) {
  347.   
  348.     // retrieve the next node that needs to be processed
  349.     var child = item.childNodes[i];
  350.     
  351.     // always enable this node and remove the
  352.     // 'itemdisabled' attribute.
  353.     if (child.hasAttribute("itemdisabled")) {
  354.       child.removeAttribute("itemdisabled");
  355.     }
  356.     child.removeAttribute("disabled");
  357.  
  358.     // recursively step down the hierarchy if this node
  359.     // exposes any further child nodes.
  360.     if (child.childNodes.length > 0) {
  361.       EnableHierarchy(child);
  362.     }
  363.   }
  364. }
  365.